#include "classe.h"

#include <stdio.h>

Player::Player(void){
    life = 100;
    label(LABEL_PLAYER);


    light.spot();
    light.color(0,100,0);
    light.intensity(100);

    linearSpeed(3.0);
    lifebar.load("media/sprites/lifebar.bmp");
    lifebar.scale(1.0,0.5);
    lifebar.position(10 + lifebar.width()/2,10 + lifebar.height()/2);

    lifebar_empty.load("media/sprites/lifebar_empty.bmp");
    lifebar_empty.scale(1.0,0.5);
    lifebar_empty.position(10 + lifebar_empty.width()/2,10 + lifebar_empty.height()/2);


}

int Player::collide(Object &other){
    if (other.label() == LABEL_ENEMY){
        if (life > 0)
        {
            life--;
            lifebar.scale(life/100.0,0.5);
            lifebar.position(10 + lifebar.width()/2,10 + lifebar.height()/2);

            if (life <= 0)
            {
                control().disableAll();
                disableMouse();
            }
        }
    }
    if (other.label() == LABEL_OCEAN && other.label() != LABEL_SURFACE){
        life-=10;
        lifebar.scale(life/100.0,0.5);
        lifebar.position(10 + lifebar.width()/2,10 + lifebar.height()/2);

        if (life <= 0)
        {
            control().disableAll();
            disableMouse();
        }
    }
    return 1;
}



Enemy::Enemy(void){
    life = 100;
    label(LABEL_ENEMY);
    load("media/models/mask/mask.md2");
    changeAnimation(Animation::WALK);
    graphic().setAnimationDelay(Animation::WALK,10);
    graphic().setAnimationDelay(Animation::TAUNT02,8);
    graphic().setAnimationDelay(Animation::HIT02,6);
    changeBoundingVolume(Solid::OBOX);
    animatedVolume();
    scale(2.0);
    position(-30,16.5,0);
}

int Enemy::collide(Object &other){
    if (other.label() == LABEL_PORTALBALL)
    {
        if (life > 0){
            life -= 10;
            tempAnimation(Animation::HIT02, Animation::WALK);
        }
        else{
            visible(false);
            inactiveBody();
        }
    }
    return 1;
}

Portal::Portal(){

}

void Portal::act(void)
{
    light.position() = position();
    timer--;
    if (timer <= 0)    disappear();
}

void Portal::appear(void){
    timer = 2000;
    visible(true);
}

void Portal::disappear(void){
    timer = 0;
    visible(false);
}



PortalIn::PortalIn(){
     //scale(5.0);
    //load("media/tex/portal.png");
    sphere();
    scale(5,7,0);
    loadTexture("media/tex/portalAzul.png");

    light.point();
    light.color(0,0,255);
    light.intensity(100);
    light.attenuation(0.2);
    light.quadraticAttenuation(0.01);

    disappear();
}

int PortalIn::collide(Object &other){
    if (other.label() == LABEL_ENEMY|| other.label() == LABEL_PLAYER || other.label() == LABEL_OBJECT){
        other.body().pos = positionOut;
        disappear();
    }
return 1;
}

void PortalIn::setPositionOut(Math::Array positionout){
    //this.positionOut = positionOut;
    positionOut = positionout;
}

/*Math::Array PortalIn::getPositionOut(void){
    return positionOut;
}*/

PortalOut::PortalOut(){
    //scale(5.0);
    //load("media/tex/portal.png");
    //sphere();
    sphere();
    scale(5,7,0);
    loadTexture("media/tex/portalLaranja.png");

    light.point();
    light.color(255,165,0);
    light.intensity(100);
    light.attenuation(0.2);
    light.quadraticAttenuation(0.01);

    disappear();
}

int PortalOut::collide(Object &other){
    if (other.label() == LABEL_ENEMY|| other.label() == LABEL_PLAYER || other.label() == LABEL_OBJECT){
        //soundBlock();
         disappear();
    }
return 1;
}


Shot::Shot(void){
    sphere();
    changeBoundingVolume(Solid::SPHERE);
    label(LABEL_PORTALBALL);
    scale(2.0);
    body().gravityScale(0.0);
    body().damping(0.0);

    light.color(255,180,60);
    light.intensity(5.0);
    light.attenuation(0.4);
    light.quadraticAttenuation(0.01);


    visible(false);
    disappear();

    p.generate(Particle::GLOW);
    p.setAnimationType(Particle::FIRE);
    p.color(130,70,20);
    p.scale(3.25);

    spark.load("media/sprites/spark.tga");
    spark.setAnimationType(Particle::FOG);
    spark.color(200,100,40);
    spark.scale(3.5);
    spark.animationMotionOffset(3.5);
    spark.stop();
}

void Shot::act(void)
{
    p.position() = position();
    light.position() = position();

    if (p.isEmitting())
    {
        timer--;
        light.intensity(float(timer)*0.0715);
        if (timer <= 0)    disappear();
    }
    if(mouseButton == LEFT_BUTTON && portalblue.visible())  portalblue.disappear();
    if(mouseButton == RIGHT_BUTTON && portalorange.visible())  portalorange.disappear();
    //if(!portalorange.visible()) lastPostion impedir bug quando portal laranja some
}

int Shot::collide(Object &other){
    if (other.label() == LABEL_ENEMY)
    {
        spark.start(10);
        spark.position() = position();
        disappear();
    }
    //if(other.label() == LABEL_WALL || other.label() == LABEL_FLOOR){
    if(other.label() == LABEL_SURFACE){
        if(mouseButton == LEFT_BUTTON){
            mouseButton = NONE_BUTTON;
            portalblue.appear();
            portalblue.position() = position();
            //if(other.label()== LABEL_WALL)  portalblue.direction(0,1,0);
            portalblue.direction(-1,0,0);
            disappear();
        }
        if(mouseButton == RIGHT_BUTTON){
            Math::Array shift(1, 1, 0);
            mouseButton = NONE_BUTTON;
            portalorange.appear();
            portalorange.position() = position();
            portalblue.setPositionOut(position() + shift);
            portalorange.direction(-1,0,0);
            disappear();
        }
    }
    return 1;
}

void Shot::appear(void){
    timer = 70;
    light.intensity(float(timer)*0.0715);
    p.start();
    activeBody();
}

void Shot::disappear(void){
    light.intensity(0.0);
    timer = 0;
    visible(false);
    p.stop();
}


Wall::Wall(void){
    box();
    label(LABEL_SURFACE);
    loadTexture("media/tex/bricks.bmp");
    staticBody();
}


